package
        com.zzc.sale.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hos.entity.stock.Medicines;
import com.hos.entity.stock.Producer;
import com.hos.entity.sys.User;
import com.hos.utils.WebUtil;
import com.zzc.sale.feigns.UserFeign;
import com.zzc.sale.mapper.ProducerMapper;
import com.zzc.sale.service.ProducerService;
import com.zzc.sale.vo.ProducerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author：xIAO_ZHOU_TX
 * @date：Created in 2023/12/12 15:20
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class ProducerServiceImpl implements ProducerService {
    @Resource
    private ProducerMapper producerMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private UserFeign userFeign;

    @Override
    public List<Producer> selectAllProducter() {

    return producerMapper.selectList(null);
    }

    @Override
    public Page<Producer> listProducterForPage(ProducerVo producerVo) {
        Page<Producer> page =new Page<>(producerVo.getPageNum(),producerVo.getPageSize());
        LambdaQueryWrapper<Producer> wrapper =new LambdaQueryWrapper<>();
        //厂家名称
        if(StringUtils.hasText(producerVo.getProducerName())){
            wrapper.like(Producer::getProducerName,producerVo.getProducerName());
        }
        //关键字
        if(StringUtils.hasText(producerVo.getKeywords())){
            wrapper.like(Producer::getKeywords,producerVo.getKeywords());
        }
        //厂家电话
        if(StringUtils.hasText(producerVo.getProducterTel())){
            wrapper.like(Producer::getProducerTel,producerVo.getProducterTel());
        }
        //状态
        if(StringUtils.hasText(producerVo.getStatus())){
            wrapper.eq(Producer::getStatus,producerVo.getStatus());
        }
        //创建时间
        if (Objects.nonNull(producerVo.getDateRange())) {
            wrapper.between(Producer::getCreateTime, producerVo.getDateRange()[0], producerVo.getDateRange()[1]);
        }
        return producerMapper.selectPage(page,wrapper);
    }

    @Override
    public Integer addProducter(Producer producers) {
        //  获取创建者信息
        String token = WebUtil.getToken();
        String userName = userFeign.selectUserByPhone(redisTemplate.opsForValue().get("user:" + token)).getUserName();
        producers.setCreateBy(userName);
        //创建时间
        producers.setCreateTime(new Date());
        return producerMapper.insert(producers);

    }

    @Override
    public Integer deleteProducterByIds(List<Long> ids) {
        return producerMapper.deleteBatchIds(ids);
    }

    @Override
    public Integer updateProducter(Producer producer) {
        // TODO
        // 获取更新者信息
        String token = WebUtil.getToken();
        String userName = userFeign.selectUserByPhone(redisTemplate.opsForValue().get("user:" + token)).getUserName();
        producer.setCreateBy(userName);

        //添加修改时间
        producer.setUpdateTime(new Date());
        return producerMapper.updateById(producer);
    }

    @Override
    public Producer getProducterById(Long producterId) {
        return producerMapper.selectById(producterId);
    }


}